﻿using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Security.Cryptography;
using YanZhiwei.DotNet2.Utilities.Models;
namespace YanZhiwei.DotNet2.Utilities.Common
{
    /// <summary>
    /// 验证码图片类
    /// </summary>
    public class VerifyImageHelper
    {
        #region 私有变量
        private static byte[] randb = new byte[4];
        private static RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider();
        private static Matrix m = new Matrix();
        private static Bitmap charbmp = new Bitmap(40, 40);
        private static Font[] fonts = {
                                        new Font(new FontFamily("Times New Roman"), 16 + NumberHelper.Random(3), FontStyle.Regular),
                                        new Font(new FontFamily("Georgia"), 16 + NumberHelper.Random(3), FontStyle.Regular),
                                        new Font(new FontFamily("Arial"), 16 + NumberHelper.Random(3), FontStyle.Regular),
                                        new Font(new FontFamily("Comic Sans MS"), 16 + NumberHelper.Random(3), FontStyle.Regular)
                                     };
        #endregion
        #region 生成验证码
        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <param name="code">验证码包含字符</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <param name="bgcolor">背景颜色</param>
        /// <param name="textcolor">文本颜色</param>
        /// <returns></returns>
        public VerifyImageInfo GenerateImage(string code, int width, int height, Color bgcolor, int textcolor)
        {
            VerifyImageInfo _verifyimage = new VerifyImageInfo();
            _verifyimage.ImageFormat = ImageFormat.Jpeg;
            _verifyimage.ContentType = "image/pjpeg";
            // 直接指定尺寸, 而不使用外部参数中的建议尺寸
            width = 90;
            height = 30;
            Bitmap _bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            Graphics _g = Graphics.FromImage(_bitmap);
            _g.SmoothingMode = SmoothingMode.HighSpeed;
            _g.Clear(bgcolor);
            int _fixedNumber = textcolor == 2 ? 60 : 0;
            Pen _linePen = new Pen(Color.FromArgb(NumberHelper.Random(50) + _fixedNumber, NumberHelper.Random(50) + _fixedNumber, NumberHelper.Random(50) + _fixedNumber), 1);
            SolidBrush _drawBrush = new SolidBrush(Color.FromArgb(NumberHelper.Random(100), NumberHelper.Random(100), NumberHelper.Random(100)));
            for (int i = 0; i < 3; i++)
            {
                _g.DrawArc(_linePen, NumberHelper.Random(20) - 10, NumberHelper.Random(20) - 10, NumberHelper.Random(width) + 10, NumberHelper.Random(height) + 10, NumberHelper.Random(-100, 100), NumberHelper.Random(-200, 200));
            }
            Graphics _charg = Graphics.FromImage(charbmp);
            float _charx = -18;
            for (int i = 0; i < code.Length; i++)
            {
                m.Reset();
                m.RotateAt(NumberHelper.Random(50) - 25, new PointF(NumberHelper.Random(3) + 7, NumberHelper.Random(3) + 7));
                _charg.Clear(Color.Transparent);
                _charg.Transform = m;
                //定义前景色为黑色
                _drawBrush.Color = Color.Black;
                _charx = _charx + 18 + NumberHelper.Random(5);
                PointF drawPoint = new PointF(_charx, 2.0F);
                _charg.DrawString(code[i].ToString(), fonts[NumberHelper.Random(fonts.Length - 1)], _drawBrush, new PointF(0, 0));
                _charg.ResetTransform();
                _g.DrawImage(charbmp, drawPoint);
            }
            _drawBrush.Dispose();
            _g.Dispose();
            _charg.Dispose();
            _verifyimage.Image = _bitmap;
            return _verifyimage;
        }
        #endregion 
    }
}